package com.cg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cg.conf.result.PageVO;
import com.cg.dto.AddLikeDTO;
import com.cg.dto.AddWorkDTO;
import com.cg.dto.SearchWorkDTO;
import com.cg.dto.UpdateWorkDTO;
import com.cg.entity.UserLike;
import com.cg.entity.Work;
import com.cg.mapper.WorkMapper;
import com.cg.result.R;
import com.cg.service.AccountService;
import com.cg.service.ILikeService;
import com.cg.service.IWorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 作品 服务实现类
 * </p>
 *
 * @author 加
 * @since 2025-01-17
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements IWorkService {
    @Autowired
    private IWorkService workService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private ILikeService likeService;
    @Override
    public R<List<Work>> getWorkByCondition(SearchWorkDTO searchWorkDTO) {
        LambdaQueryWrapper<Work> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(searchWorkDTO.getWorkName()!=null,Work::getWorkName,searchWorkDTO.getWorkName())
                .eq(searchWorkDTO.getTypeId()!=null,Work::getTypeId,searchWorkDTO.getTypeId());

        Page<Work> workPage=new Page<>(searchWorkDTO.getPageNumber(), searchWorkDTO.getPageSize());
        workPage=page(workPage,queryWrapper);

        PageVO<Work> workPageVO=new PageVO<>();
        BeanUtils.copyProperties(workPage,workPageVO);
        return R.Success(workPageVO);
    }


    @Override
    public R addLike(AddLikeDTO addLikeDTO) {
        UserLike like=new UserLike();
        like.setWorkId(addLikeDTO.getWorkId());
        like.setUserId(accountService.getCurrentUserId());
        like.setContent(workService.selectContentById(addLikeDTO.getWorkId()));
        likeService.save(like);
        return R.Success("收藏绣品成功");
    }

    @Override
    public String selectContentById(Long workId) {
        Work work=getById(workId);
        return work.getWorkName();
    }

    @Override
    public R addWork(AddWorkDTO addWorkDTO) {
        System.out.println("111"+(workService.selectWorkByName(addWorkDTO.getWorkName())));
        if(workService.selectWorkByName(addWorkDTO.getWorkName())==null||workService.selectWorkByName(addWorkDTO.getWorkName()).isEmpty()) {
            Work work = new Work();
            BeanUtils.copyProperties(addWorkDTO, work);
            save(work);
            return R.Success("新增绣品成功");
        }else{
            return R.Failed("新增失败，该绣品名称已存在");
        }
    }

    @Override
    public R updateWork(UpdateWorkDTO updateWorkDTO) {
        if(workService.selectWorkById(updateWorkDTO.getWorkId())!=null&&updateWorkDTO.getWorkId()!=null) {
            Work work = getById(updateWorkDTO.getWorkId());
            BeanUtils.copyProperties(updateWorkDTO, work);
            updateById(work);
            return R.Success("修改成功");
        }else{
            return R.Failed("修改失败，不存在该绣品");
        }
    }

    @Override
    public Work selectWorkById(Long workId) {
        Work work=getById(workId);
        if(work!=null){
            return work;
        }
        else{
            return null;
        }
    }

    @Override
    public R deleteWork(Long workId) {
        if (workService.selectWorkById(workId) != null && workId!= null) {
            Work work = getById(workId);
            removeById(work);
            return R.Success("删除成功");
        } else {
            return R.Failed("删除失败，该绣品不存在");
        }
    }

    @Override
    public List<Work> selectWorkByName(String workName) {
        LambdaQueryWrapper<Work> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Work::getWorkName, workName);
        List<Work> list = list(queryWrapper);
        if (list != null) {
            return list;
        } else {
            return null;
        }
    }
}
